package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.whut.mapper.MeasureresultMapper;
import com.whut.model.*;
import com.whut.service.ActualMeasureService;
import com.whut.utils.FileUtil;
import com.whut.utils.MDoc;
import com.whut.v_model.MeasureResultData;
import com.whut.v_model.Point;
import com.whut.model.Measuretablerecord;
import com.whut.v_model.PointGroup;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther wenyihan
 * 生成实测实量表
 * @date 15:39 2019/9/8
 * @param
 * @return
*/
@Service("actualMeasureService")
public class ActualMeasureImpl implements ActualMeasureService {
    @Resource
    private MeasureresultMapper measureresultMapper;

    @Resource
    private UrlProperties urlProperties;
    @Override
    public String ftlGetReport(int recordId) throws UnsupportedEncodingException {
        Map<String, Object> dataMap = new HashMap<>();

        List<Measuretablerecord> projectNameList = measureresultMapper.getProjectName(recordId);
        String projectName = JSON.toJSONString(projectNameList, SerializerFeature.WriteMapNullValue);
        JSONArray projectNameArray = JSONArray.parseArray(projectName);
        JSONObject projectNameObject = projectNameArray.getJSONObject(0);
        dataMap.put("projectName", projectNameObject.getString("projectName"));
        dataMap.put("finishTime", projectNameObject.getString("finishTime"));

        List<Measureresult> formDataList = measureresultMapper.getFromData(recordId);

        int n = 0; //检查部位 checkP percent pointN
        int t = 0; //评判标准 checkT rangeM
        int g = 0; //pointData scD jsD pcD
        int m = 0; //sjD jsD(type == 2)
        int type = 0;
        for (Measureresult mr: formDataList) {
            type = mr.getType();
            n++;
            if (!mr.getIsEmpty()){
                dataMap.put("checkP"+n, mr.getCheckPart());
                dataMap.put("percent"+n, mr.getQualifiedRate());
                int resultTypeId = mr.getResultTypeId();
                List<Measureresulttype> resultType = measureresultMapper.getResultType(resultTypeId);
                for (Measureresulttype mrt:resultType) {
                    t++;
                    System.out.println("checkT:"+mrt.getParameterName());
                    dataMap.put("checkT" + t, mrt.getParameterName());
                    dataMap.put("rangeM" + t, mrt.getRangeName());
                }

                //pointData
                String pointData = mr.getPointData();
                MeasureResultData measureResultData = JSON.parseObject(pointData, MeasureResultData.class);
                dataMap.put("pointN"+n, measureResultData.getTotalPointNum());

                //groups
                List<PointGroup> groups = measureResultData.getGroups();
                m = 0;
                g = 0;
                for (PointGroup pg: groups) {
                    m++;
                    List<Point> groupData = pg.getGroupData();
                    for (Point p: groupData) {
                        g++;
                        if (type == 0 || type == 1) {
                            dataMap.put("scD"+g, p.getObservedValue());
                            dataMap.put("jsD"+g, p.getOffset());
                            dataMap.put("pcD"+g, p.getOffset());
                            dataMap.put("sjD"+g, pg.getDesignValue());
                            if (n == 1) {
                                dataMap.put("scD"+g+"h", p.getObservedValue());
                                dataMap.put("jsD"+g+"h", p.getOffset());
                                dataMap.put("pcD"+g+"h", p.getOffset());
                                dataMap.put("sjD"+g+"h", pg.getDesignValue());
                            }else if (n == 2) {
                                dataMap.put("scD"+g+"w", p.getObservedValue());
                                dataMap.put("jsD"+g+"w", p.getOffset());
                                dataMap.put("pcD"+g+"w", p.getOffset());
                                dataMap.put("sjD"+g+"w", pg.getDesignValue());
                            }else if (n == 3) {
                                dataMap.put("scD"+g+"z", p.getObservedValue());
                                dataMap.put("jsD"+g+"z", p.getOffset());
                                dataMap.put("pcD"+g+"z", p.getOffset());
                                dataMap.put("sjD"+g+"z", pg.getDesignValue());
                            }else if (n == 4) {
                                dataMap.put("scD"+g+"y", p.getObservedValue());
                                dataMap.put("jsD"+g+"y", p.getOffset());
                                dataMap.put("pcD"+g+"y", p.getOffset());
                                dataMap.put("sjD"+g+"y", pg.getDesignValue());
                            }
                        }else if (type == 2){
                            dataMap.put("scD"+g, p.getObservedValue());
                            dataMap.put("pcD"+g, pg.getMaxOffset());
                            if (n == 1) {
                                dataMap.put("scD"+g+"h", p.getObservedValue());
                                dataMap.put("sjD"+g+"h", pg.getDesignValue());
                                dataMap.put("jsD"+g+"h", pg.getMaxOffset());
                                dataMap.put("pcD"+g+"h", pg.getMaxOffset());
                            }else if (n == 2) {
                                dataMap.put("scD"+g+"w", p.getObservedValue());
                                dataMap.put("sjD"+g+"w", pg.getDesignValue());
                                dataMap.put("jsD"+g+"w", pg.getMaxOffset());
                                dataMap.put("pcD"+g+"w", pg.getMaxOffset());
                            }else if (n == 3) {
                                dataMap.put("scD"+g+"z", p.getObservedValue());
                                dataMap.put("sjD"+g+"z", pg.getDesignValue());
                                dataMap.put("jsD"+g+"z", pg.getMaxOffset());
                                dataMap.put("pcD"+g+"z", pg.getMaxOffset());
                            }else if (n == 4) {
                                dataMap.put("scD"+g+"y", p.getObservedValue());
                                dataMap.put("sjD"+g+"y", pg.getDesignValue());
                                dataMap.put("jsD"+g+"y", pg.getMaxOffset());
                                dataMap.put("pcD"+g+"y", pg.getMaxOffset());
                            }
                        }
                    } // for Point
                    if (type == 2){
                        dataMap.put("sjD"+m, pg.getDesignValue());
                        dataMap.put("jsD"+m, pg.getMaxOffset());
                        if (n == 1) {
                            dataMap.put("sjD"+m+"h", pg.getDesignValue());
                            dataMap.put("jsD"+m+"h", pg.getMaxOffset());
                        }else if (n == 2) {
                            dataMap.put("sjD"+m+"w", pg.getDesignValue());
                            dataMap.put("jsD"+m+"w", pg.getMaxOffset());
                        }else if (n == 3) {
                            dataMap.put("sjD"+m+"z", pg.getDesignValue());
                            dataMap.put("jsD"+m+"z", pg.getMaxOffset());
                        }else if (n == 4) {
                            dataMap.put("sjD"+m+"y", pg.getDesignValue());
                            dataMap.put("jsD"+m+"y", pg.getMaxOffset());
                        }
                    }
                } //for PointGrou
            } else{
                dataMap.put("checkP"+n, "");
                dataMap.put("percent"+n, "");
                int resultTypeId = mr.getResultTypeId();
                List<Measureresulttype> resultType = measureresultMapper.getResultType(resultTypeId);
                for (Measureresulttype mrt:resultType) {
                    t++;
                    dataMap.put("checkT" + t, "");
                    dataMap.put("rangeM" + t, "");
                }

                //pointData
                String pointData = mr.getPointData();
                MeasureResultData measureResultData = JSON.parseObject(pointData, MeasureResultData.class);
                dataMap.put("pointN"+n, "");

                //groups
                for (int o = 0; o < 70 ; o++) {
                    for (int k = 0; k < 70; k++) {
                        if (type == 0 || type == 1){
                            dataMap.put("scD"+k, "");
                            dataMap.put("jsD"+k, "");
                            dataMap.put("pcD"+k, "");
                            dataMap.put("sjD"+k, "");
                            if (n == 1) {
                                dataMap.put("scD"+k+"h", "");
                                dataMap.put("jsD"+k+"h", "");
                                dataMap.put("pcD"+k+"h", "");
                                dataMap.put("sjD"+k+"h", "");
                            }else if (n == 2) {
                                dataMap.put("scD"+k+"w", "");
                                dataMap.put("jsD"+k+"w", "");
                                dataMap.put("pcD"+k+"w", "");
                                dataMap.put("sjD"+k+"w", "");
                            }else if (n == 3) {
                                dataMap.put("scD"+k+"z", "");
                                dataMap.put("jsD"+k+"z", "");
                                dataMap.put("pcD"+k+"z", "");
                                dataMap.put("sjD"+k+"z", "");
                            }else if (n == 4) {
                                dataMap.put("scD"+k+"y", "");
                                dataMap.put("jsD"+k+"y", "");
                                dataMap.put("pcD"+k+"y", "");
                                dataMap.put("sjD"+k+"y", "");
                            }
                        }else if (type == 2){
                            dataMap.put("scD"+k, "");
                            dataMap.put("pcD"+k, "");
                            if (n == 1) {
                                dataMap.put("scD"+k+"h", "");
                                dataMap.put("pcD"+k+"h", "");
                            }else if (n == 2) {
                                dataMap.put("scD"+k+"w", "");
                                dataMap.put("pcD"+k+"w", "");
                            }else if (n == 3) {
                                dataMap.put("scD"+k+"z", "");
                                dataMap.put("pcD"+k+"z", "");
                            }else if (n == 4) {
                                dataMap.put("scD"+k+"y", "");
                                dataMap.put("pcD"+k+"y", "");
                            }
                        }
                    } // for Point
                    if (type == 2){
                        dataMap.put("sjD"+o, "");
                        dataMap.put("jsD"+o, "");
                        if (n == 1) {
                            dataMap.put("sjD"+o+"h", "");
                            dataMap.put("jsD"+o+"h", "");
                        }else if (n == 2) {
                            dataMap.put("sjD"+o+"w", "");
                            dataMap.put("jsD"+o+"w", "");
                        }else if (n == 3) {
                            dataMap.put("sjD"+o+"z", "");
                            dataMap.put("jsD"+o+"z", "");
                        }else if (n == 4) {
                            dataMap.put("sjD"+o+"y", "");
                            dataMap.put("jsD"+o+"y", "");
                        }
                    }
                } //for PointGroup
            } //if isEmpty()
        } //for Measureresult

        //补充填满之后全部的占位符
        for (int i = (n+1); i < 5; i++){
            dataMap.put("checkP"+i, "");
            dataMap.put("percent"+i, "");
            dataMap.put("pointN"+i, "");
        }
        for (int i = (t+1); i < 8; i++){
            dataMap.put("checkT"+i, "");
            dataMap.put("rangeM"+i, "");
        }
        if (type == 2){
            for (int i = (m+1); i < 70; i++){
                dataMap.put("sjD"+i, "");
                dataMap.put("jsD"+i, "");
                dataMap.put("sjD"+i+"h", "");
                dataMap.put("jsD"+i+"h", "");
                dataMap.put("sjD"+i+"w", "");
                dataMap.put("jsD"+i+"w", "");
                dataMap.put("sjD"+i+"z", "");
                dataMap.put("jsD"+i+"z", "");
                dataMap.put("sjD"+i+"y", "");
                dataMap.put("jsD"+i+"y", "");
            }
            for (int i = (g+1); i < 70; i++){
                dataMap.put("scD"+i, "");
                dataMap.put("pcD"+i, "");
                dataMap.put("scD"+i+"h", "");
                dataMap.put("pcD"+i+"h", "");
                dataMap.put("scD"+i+"w", "");
                dataMap.put("pcD"+i+"w", "");
                dataMap.put("scD"+i+"z", "");
                dataMap.put("pcD"+i+"z", "");
                dataMap.put("scD"+i+"y", "");
                dataMap.put("pcD"+i+"y", "");
            }
        } else if (type == 1 || type == 0){
            for (int i = (g+1); i < 70; i++){
                dataMap.put("scD"+i, "");
                dataMap.put("jsD"+i, "");
                dataMap.put("pcD"+i, "");
                dataMap.put("sjD"+i, "");
                dataMap.put("scD"+i+"h", "");
                dataMap.put("jsD"+i+"h", "");
                dataMap.put("pcD"+i+"h", "");
                dataMap.put("sjD"+i+"h", "");
                dataMap.put("scD"+i+"w", "");
                dataMap.put("jsD"+i+"w", "");
                dataMap.put("pcD"+i+"w", "");
                dataMap.put("sjD"+i+"w", "");
                dataMap.put("scD"+i+"z", "");
                dataMap.put("jsD"+i+"z", "");
                dataMap.put("pcD"+i+"z", "");
                dataMap.put("sjD"+i+"z", "");
                dataMap.put("scD"+i+"y", "");
                dataMap.put("jsD"+i+"y", "");
                dataMap.put("pcD"+i+"y", "");
                dataMap.put("sjD"+i+"y", "");
            }
        }
        dataMap.put("checkP", dataMap.get("checkP1"));
        dataMap.put("percent", dataMap.get("percent1"));
        dataMap.put("pointN", dataMap.get("pointN1"));
        dataMap.put("checkT", dataMap.get("checkT1"));
        dataMap.put("rangeM", dataMap.get("rangeM1"));

        System.out.println("dataMap:" + dataMap);

        int mKey = measureresultMapper.getmKey(recordId);
        Date date = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = d1.format(date) + mKey;
        MDoc mdoc = new MDoc();
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }
        mdoc.createDoc(dataMap, path + "\\doc\\" + fileName + ".doc", "scslFTL\\"+mKey+".ftl");
        //MDoc.docToDocx("D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + fileName + ".doc", "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + fileName + ".doc");
        String result = urlProperties.getAddress()+"/doc/" + fileName + ".doc";
        return result;
        /*MDoc mdoc = new MDoc();
        int mKey = measureresultMapper.getmKey(recordId);
        mdoc.createDoc(dataMap, "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + fileName + ".doc", "scslFTL\\"+mKey+".ftl");
        MDoc.docToDocx("E:\\tencent_File\\1296928795\\FileRecv\\yirui" + fileName + ".doc", "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + fileName + ".doc");
        String result = "E:\\tencent_File\\1296928795\\FileRecv\\yirui" + fileName + ".doc";*//*
        int mKey = measureresultMapper.getmKey(recordId);
        Date date = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = d1.format(date) + mKey;

        MDoc mdoc = new MDoc();
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }
        mdoc.createDoc(dataMap, path + "\\doc\\" + fileName + ".doc", "scslFTL\\"+mKey+".ftl");
        MDoc.docToDocx("D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + fileName + ".doc", "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + fileName + ".doc");
        String result = urlProperties.getAddress()+"/doc/" + fileName + ".doc";
        return result;*/

    }
}
